
package database.daos;

import database.orm.ORMException;
import database.databasemanager.exceptions.DataBaseException;

import beans.UserTypeBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class UserTypeDAO extends AbstractDAO <UserTypeBean>{

    @Override
    public void insert(UserTypeBean usrType) throws DAOException {
        try {
                dataBaseManager.openConnection();
                String sqlStatement = "INSERT INTO USER_TYPE(userTypeId,userTypeDescription)values('" + usrType.getUserTypeId() + "','" + usrType.getTypeDescription() + "');";
                dataBaseManager.executeUpdate(sqlStatement);
        }catch (DataBaseException exception) {
                throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
             try {
                    dataBaseManager.closeConnection();
             } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
             }
        }
    }

    @Override
    public void delete(String conticion) throws DAOException {
       try {
           dataBaseManager.openConnection();
           String sqlStatement = "DELETE FROM user_type " + conticion;
           dataBaseManager.executeUpdate(sqlStatement);
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
             try {
                   dataBaseManager.closeConnection();
             } catch (DataBaseException exception) {
                   throw new DAOException(exception.getExceptionMessage(),exception);
             }
        }
    }

    @Override
    public void update(UserTypeBean usrType, String condition) throws DAOException {
       try {
            dataBaseManager.openConnection();
            String sqlStatement = "UPDATE user_type SET userTypeDescription='" + usrType.getTypeDescription() + "',userTypeId='" + usrType.getUserTypeId() + "' " + condition;
            dataBaseManager.executeUpdate(sqlStatement);
        }catch (DataBaseException exception) {
                throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
             try {
                     dataBaseManager.closeConnection();
             } catch (DataBaseException exception) {
                     throw new DAOException(exception.getExceptionMessage(),exception);
             }
        }
    }

    @Override
    protected ArrayList<UserTypeBean> searchAll(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM USER_TYPE " + condition;
            ArrayList<UserTypeBean> userTypeList = null;
            userTypeList = createUserTypeBeanList(dataBaseManager.executeQuery(sqlStatement));
            return userTypeList;
        }catch (DataBaseException exception) {
             throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
             try {
                     dataBaseManager.closeConnection();
             } catch (DataBaseException exception) {
                     throw new DAOException(exception.getExceptionMessage(),exception);
             }
        }
    }







    @Override
    protected UserTypeBean search(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM USER_TYPE " + condition;
            UserTypeBean userTypeBean = null;
            userTypeBean = createUserTypeBean(dataBaseManager.executeQuery(sqlStatement));
            return userTypeBean;
        }catch (DataBaseException exception) {
                throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
             try {
                    dataBaseManager.closeConnection();
             } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
             }
        }
    }



    private ArrayList<UserTypeBean> createUserTypeBeanList(ResultSet queryResult) throws DAOException{
        try{
                ArrayList<UserTypeBean> storeProviderBeanList = new ArrayList();
                while(queryResult.next()){
                    storeProviderBeanList.add((UserTypeBean) beansGenerator.generateBean(queryResult, UserTypeBean.class));
                }
                return storeProviderBeanList;
         } catch (SQLException exception) {
                throw new DAOException(exception.getMessage(),exception);
        } catch (ORMException exception){
                throw new DAOException(exception.getExceptionMessage(),exception);
        }
    }

    private UserTypeBean createUserTypeBean(ResultSet queryResult) throws DAOException{
        try{
            moveQueryResultToBegin(queryResult);
            return (UserTypeBean) beansGenerator.generateBean(queryResult, UserTypeBean.class);
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        } catch (ORMException exception){
                throw new DAOException(exception.getExceptionMessage(),exception);
        }
    }



     public void moveQueryResultToBegin(ResultSet queryResult) throws SQLException{
        queryResult.first();
    }



    

}
